<?php

/********************************/
class InvoiceService {
/********************************/
    private static $wsdl = "https://test.e-szamla.hu/soap/InvoiceService.wsdl";
    private static $client = null;
    private static $sid = "";

    public static function setSID($sid) {
        self::$sid = $sid;
        self::$client = new SoapClient(self::$wsdl);
    }

    public static function uploadInvoice(
        $partner,
        $xml,
        $meta           = LEAVE_DEFAULT,
        $displayable    = LEAVE_DEFAULT,
        $accomplished   = LEAVE_DEFAULT,
        $pro_forma      = LEAVE_DEFAULT,
        $notify         = LEAVE_DEFAULT,
        $pid            = LEAVE_DEFAULT
    ) {
        $request = array(
            sessionid => self::$sid,
            partner   => $partner,
            xml       => "" . $xml,
        );
        if ($displayable  == LEAVE_DEFAULT) { $request['displayable'] = 1; }
        if ($accomplished == LEAVE_DEFAULT) { $request['accomplished'] = 0; }
        if ($pro_forma    == LEAVE_DEFAULT) { $request['pro_forma'] = 0; }
        if ($notify       == LEAVE_DEFAULT) { $request['notify'] = 1; }
        if ($meta         != LEAVE_DEFAULT) { $request['meta'] = new SoapVar($meta, XSD_ANYXML); }

        $result = self::$client->uploadInvoice($request);
    }

    public static function getInvoices(InvoiceFilter $filterData) {
        $request = $filterData->get();
        $request['sessionid'] = self::$sid;
        $result = self::$client->getInvoices($request);
        return $result->invoice;
    }

/*
    public static function getInvoice(
        $invoicenumber,
        $partner,
        $invoiceformat  = LEAVE_DEFAULT,
        $pro_forma      = LEAVE_DEFAULT
    ) {
        if ($invoiceformat == LEAVE_DEFAULT) { $invoiceformat = "pdf"; }
        if ($pro_forma     == LEAVE_DEFAULT) { $pro_forma = 0; }

        $result = self::$client->getInvoice(array(
            sessionid        => self::$sid,
            "invoice-number" => $invoicenumber,
            "invoice-format" => $invoiceformat,
            partner          => $partner,
            pro_forma        => $pro_forma,
        ));
        return $result->content;
    }
*/    
    public static function pay(
        $partner,
        $invoicenumber,
        $amount,
        $currency,
        $return_url
    ) {
        $invoiceData = getConfigParameter("site", "invoiceData");
        $result = self::$client->pay(array(
            sessionid        => self::$sid,
            partner          => $partner,
            "invoice-number" => $invoicenumber,
            bank_id          => $invoiceData['bank_id'],
            amount           => $amount,
            currency         => $currency,
            return_url       => $return_url,
        ));
        return $result->url;
    }

    public static function getOnlinePayments(OnlinePaymentsFilter $filterData) {
        $result = $filterData->get();
        $result['sessionid'] = self::$sid;
        $result = self::$client->getOnlinePayment($filterData->get());
        return $result->payment;
    }

    public static function getInvoice($invoiceNumber) {
        $invoiceData = getConfigParameter("site", "invoiceData");
        $result = self::$client->getInvoice(array(
            sessionid        => self::$sid,
            partner          => $invoiceData['username'],
            "invoice-number" => $invoiceNumber,
            "invoice-format" => "pdf",
        ));
        return $result->content;
    }
}

?>
